package com.aim;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

@Slf4j
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class BootApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(BootApplication.class);
    }

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext run = SpringApplication.run(BootApplication.class, args);
        String port = getParam("POST", run.getEnvironment().getProperty("server.port"));
        log.info(String.format(" - Current system file encoding: %s", System.getProperty("file.encoding")));
        log.info(String.format(" - Current sun jnu encoding: %s", System.getProperty("sun.jnu.encoding")));
        log.info("\n---------------------------------------------------------\n" +
                "Application Admin is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + "/\n\t" +
                "External:\thttp://" + InetAddress.getLocalHost().getHostAddress() + ":" + port + "/" +
                "\n-----------------页面请部署 admin-web----------------------");
    }

    private static String getParam(String param, String defVal) {
        Optional<String> envPort = Optional.ofNullable(System.getenv(param));
        return envPort.map(String::valueOf).orElse(defVal);
    }
}
